//
//  YHStatusViewModel.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/25.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHStatusViewModel: NSObject {
    
    var status: YHStatus?
    
    var verifiedImage: UIImage?
    var mbrankImage: UIImage?
    var headImageURL: NSURL?
    var sourceText: String?
    //底部工具条数据
    var forwardText: String?
    var commentText: String?
    var likeText: String?
    //原创微博属性字符串
    var originalAttributeString: NSAttributedString?
    
    
    //设置计算型属性更新时间
    var time: String? {
        return NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
    }
        
    init(status: YHStatus) {
        super.init()
        self.status = status
        headImageURL = NSURL(string: status.user?.profile_image_url ?? "")
        dealVerifiedImage()
        dealMbrankImage()
        //微博来源
        dealSourceText()
        //设置工具条数据
        forwardText = dealToolBarText(status.comments_count, defaultString: "转发")
        commentText = dealToolBarText(status.reposts_count, defaultString: "评论")
        likeText = dealToolBarText(status.attitudes_count, defaultString: "赞")
        dealWeiboText()
    
    
    }

}

extension YHStatusViewModel {
    
    func dealWeiboText() {
        let str = status?.text ?? ""
        let pattern = "\\[.*?\\]"
        let regex = try!NSRegularExpression(pattern: pattern, options: [])
        let results = regex.matchesInString(str, options: [], range: NSRange(location: 0,length: str.characters.count))
        for result in results.reverse() {
            let range = result.rangeAtIndex(0)
            let emotionStr = (str as NSString).substringWithRange(range)
            let em = findEmotionWithChs(emotionStr)
            
        }
        
    }
    
    private func findEmotionWithChs(chs: String) -> YHEmotions?{
        
        for p in YHEmotionManager.sharedEmotion.packages {
            for emotions in p.sectionEmotion {
              let array = emotions.filter({ (em) -> Bool in
                    
                    return chs == em.chs
                })
                
                if array.count > 0 {
                    return array.first
                }
            }
        }
        
        return nil
    }
    
    func dealToolBarText(count: Int, defaultString: String) -> String {
        //测试数据
//        let count = 10000
        
        if count <= 0 {
            return defaultString
        }
        
        if count >= 10000 {
            return "\(Double(count / 1000) / 10)万"
        }
        
        return "\(count)"
    }
    

//    <a href="http://app.weibo.com/t/feed/3o33sO" rel="nofollow">iPhone 6</a>
    private func dealSourceText() {
        
        let startFlag = "\">"
        let endFlag = "</a>"
        let str = status?.source ?? ""
        let startRange = str.rangeOfString(startFlag)
        let endRange = str.rangeOfString(endFlag)
        if let start = startRange, end = endRange {
            let range = start.endIndex..<end.startIndex
            sourceText = str.substringWithRange(range)
            
        }
    }
    
    private func dealVerifiedImage(){
        
        let verifiedType = status?.user?.verified ?? 0
        
        if verifiedType == 1 {
            
            verifiedImage = UIImage(named: "avatar_vip")
            
        }
    }
    
    private func dealMbrankImage(){
        
        let mbrankType = status?.user?.mbrank ?? 0
        
        if mbrankType > 0 && mbrankType < 7 {
            let image = UIImage(named: "common_icon_membership_level\(mbrankType)")
            mbrankImage = image
            
        }

        
    }
}